package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.dto.ProDto;
import com.example.dto.ProNormDto;
import com.example.dto.ProTypeDto;
import com.example.service.ProService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("pro")
public class ProController {
    @Autowired
    private ProService service;

    @RequestMapping("add")
    public Object add(@RequestBody ProDto dto) {
        dto.setCreateTime(LocalDateTime.now());

        String[] valueList = dto.getNormList();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < valueList.length; i++) {
            if (i > 0) {
                sb.append("，");
            }
            sb.append(valueList[i]);
        }
        dto.setProNorm(sb.toString());
        service.add(dto);
        return null;
    }

    @RequestMapping("select")
    public IPage<ProDto> select(@RequestBody ProDto dto) {
        IPage<ProDto> page = service.select(dto);
        List<ProDto> list = page.getRecords();
        for (ProDto proDto : list) {
            String norm = proDto.getProNorm();
            proDto.setNormList(norm.split("[，,]"));
        }
        page.setRecords(list);
        return page;
    }
    @RequestMapping("selectName")
    public List<Map<String,Object>> selectName() {
        List<Map<String,Object>>listmap = service.selectName();
//        System.out.println(listmap);
        return listmap;
    }
    @RequestMapping("update")
    public Object update(@RequestBody ProDto dto) {

        String[] valueList = dto.getNormList();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < valueList.length; i++) {
            if (i > 0) {
                sb.append("，");
            }
            sb.append(valueList[i]);
        }
        dto.setProNorm(sb.toString());
        service.update(dto);
        return null;
    }

    @RequestMapping("delete")
    public Object delete(@RequestBody ProDto dto) {
        service.delete(dto);
        return null;
    }
}
